package com.web.servlet.utils;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;


/**
 * Jedis的简单封装, 1.将连接池的操作封装起来，调用时不需要申请和释放连接，减少出错的可能性 2.可以单独添加针对redis的监控
 * 3.redis做集群时针对调用方透明 4.已经支持String,List,Set,Hash
 * 
 * @author gavin
 * 
 */
public class RedisHelper {

	/*
	 * 目前只维护一个pool.TODO:支持多个 将jedis进行鉴定的封装，方便使用，以及扩展
	 */
	private JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	private JedisPool jedisPool = null;
	private String redisIp;
	private int redisPort;
	private int redisPoolSize;
	private int database;

	public JedisPoolConfig getJedisPoolConfig() {
		return jedisPoolConfig;
	}

	public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
		this.jedisPoolConfig = jedisPoolConfig;
	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	public String getRedisIp() {
		return redisIp;
	}

	public void setRedisIp(String redisIp) {
		this.redisIp = redisIp;
	}

	public int getRedisPort() {
		return redisPort;
	}

	public void setRedisPort(int redisPort) {
		this.redisPort = redisPort;
	}

	public int getRedisPoolSize() {
		return redisPoolSize;
	}

	public void setRedisPoolSize(int redisPoolSize) {
		this.redisPoolSize = redisPoolSize;
	}

	public int getDatabase() {
		return database;
	}

	public void setDatabase(int database) {
		this.database = database;
	}

	public RedisHelper(String redisIp, int redisPort, int redisPoolSize,
			String password) {
		this(redisIp, redisPort, redisPoolSize, Protocol.DEFAULT_DATABASE,
				password);
	}

	public RedisHelper(String redisIp, int redisPort, int redisPoolSize,
			int database, String password) {

		this.redisIp = redisIp;
		this.redisPort = redisPort;
		this.redisPoolSize = redisPoolSize;

		jedisPoolConfig.setMaxTotal(1);

		jedisPool = new JedisPool(jedisPoolConfig, redisIp, redisPort,Protocol.DEFAULT_TIMEOUT, password, database);

	}

	/*
	 * 从连接池中获取连接，出错返回null
	 */
	public Jedis getJedis() {

		Jedis jedis = jedisPool.getResource();
		jedis.select(1);
		/***
		 * try { jedis = jedisPool.getResource(); } catch
		 * (JedisConnectionException e) {
		 * 
		 * }
		 ****/
		return jedis;
	}

	/*
	 * 将jedis连接放回池中
	 */
	protected void returnJedis(Jedis jedis) {
		jedisPool.returnResource(jedis);
	}

	protected void returnBrokenJedis(Jedis jedis) {
		jedisPool.returnBrokenResource(jedis);
	}

	public String set(final String key, String value) {
		Jedis jedis = getJedis();
		String reply = null;
		try {
			reply = jedis.set(key, value);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String set(final String key, final String value, final String nxxx,
			final String expx, final long time) {
		Jedis jedis = getJedis();
		String reply = null;
		try {
			reply = jedis.set(key, value, nxxx, expx, time);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String get(final String key) {
		Jedis jedis = getJedis();
		String reply = null;
		try {
			reply = jedis.get(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> mget(final String key) {
		Jedis jedis = getJedis();
		List<String> reply = null;
		try {
			reply = jedis.mget(key);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Boolean exists(final String key) {
		Jedis jedis = getJedis();

		Boolean reply = null;
		try {
			reply = jedis.exists(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}

		return reply;
	}

	public Long del(final String... keys) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.del(keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long del(String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.del(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String type(final String key) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.type(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> keys(final String pattern) {
		Jedis jedis = getJedis();

		Set<String> reply = null;

		try {
			reply = jedis.keys(pattern);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}

		return reply;
	}

	public String randomKey() {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.randomKey();
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String rename(final String oldkey, final String newkey) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.rename(oldkey, newkey);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long expire(final String key, final int seconds) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.expire(key, seconds);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long expireAt(final String key, final long unixTime) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.expireAt(key, unixTime);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long ttl(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.ttl(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> mget(final String... keys) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.mget(keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String mset(final String... keysvalues) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.mset(keysvalues);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long decrBy(final String key, final long integer) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.decrBy(key, integer);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long decr(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.decr(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long incrBy(final String key, final long integer) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.incrBy(key, integer);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long incr(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.incr(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long append(final String key, final String value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.append(key, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String substr(final String key, final int start, final int end) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.substr(key, start, end);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	/*
	 * list op
	 */
	public Long rpush(final String key, final String... strings) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.rpush(key, strings);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long lpush(final String key, final String... strings) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.lpush(key, strings);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long llen(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.llen(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> lrange(final String key, final long start,
			final long end) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.lrange(key, start, end);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String ltrim(final String key, final long start, final long end) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.ltrim(key, start, end);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String lindex(final String key, final long index) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.lindex(key, index);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String lset(final String key, final long index, final String value) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.lset(key, index, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long lrem(final String key, final long count, final String value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.lrem(key, count, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String lpop(final String key) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.lpop(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String rpop(final String key) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.rpop(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String rpoplpush(final String srckey, final String dstkey) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.rpoplpush(srckey, dstkey);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	// //-------------------------set
	public Long sadd(final String key, final String... members) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sadd(key, members);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> smembers(final String key) {
		Jedis jedis = getJedis();

		Set<String> reply = null;
		try {
			reply = jedis.smembers(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long srem(final String key, final String... members) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.srem(key, members);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String spop(final String key) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.spop(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long smove(final String srckey, final String dstkey,
			final String member) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.smove(srckey, dstkey, member);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long scard(final String key) {
		Jedis jedis = getJedis();

		Long reply = 0L;
		try {
			reply = jedis.scard(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Boolean sismember(final String key, final String member) {
		Jedis jedis = getJedis();

		Boolean reply = null;
		try {
			reply = jedis.sismember(key, member);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> sinter(final String... keys) {
		Jedis jedis = getJedis();

		Set<String> reply = null;
		try {
			reply = jedis.sinter(keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long sinterstore(final String dstkey, final String... keys) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sinterstore(dstkey, keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> sunion(final String... keys) {
		Jedis jedis = getJedis();

		Set<String> reply = null;
		try {
			reply = jedis.sunion(keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long sunionstore(final String dstkey, final String... keys) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sunionstore(dstkey, keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> sdiff(final String... keys) {
		Jedis jedis = getJedis();

		Set<String> reply = null;
		try {
			reply = jedis.sdiff(keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long sdiffstore(final String dstkey, final String... keys) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sdiffstore(dstkey, keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String srandmember(final String key) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.srandmember(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> srandmember(final String key, final int count) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.srandmember(key, count);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> sort(final String key) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.sort(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> sort(final String key,
			final SortingParams sortingParameters) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.sort(key, sortingParameters);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> blpop(final int timeout, final String... keys) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.blpop(timeout, keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> blpop(String... args) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.blpop(args);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> brpop(String... args) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.brpop(args);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> blpop(String arg) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			jedis.blpop(arg);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> brpop(String arg) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.brpop(arg);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long sort(final String key, final SortingParams sortingParameters,
			final String dstkey) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sort(key, sortingParameters, dstkey);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long sort(final String key, final String dstkey) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.sort(key, dstkey);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> brpop(final int timeout, final String... keys) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.brpop(timeout, keys);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long strlen(final String key) {
		Jedis jedis = getJedis();
		Long reply = null;
		try {
			reply = jedis.strlen(key);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long lpushx(final String key, final String... string) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.lpushx(key, string);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long persist(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.persist(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long rpushx(final String key, final String... string) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.rpushx(key, string);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String echo(final String string) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.echo(string);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long linsert(final String key, final LIST_POSITION where,
			final String pivot, final String value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.linsert(key, where, pivot, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String brpoplpush(String source, String destination, int timeout) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.brpoplpush(source, destination, timeout);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	// -------------------------Hash
	public Long hset(final String key, final String field, final String value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.hset(key, field, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String hget(final String key, final String field) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.hget(key, field);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long hsetnx(final String key, final String field, final String value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.hsetnx(key, field, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public String hmset(final String key, final Map<String, String> hash) {
		Jedis jedis = getJedis();

		String reply = null;
		try {
			reply = jedis.hmset(key, hash);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> hmget(final String key, final String... fields) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.hmget(key, fields);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long hincrBy(final String key, final String field, final long value) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.hincrBy(key, field, value);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Boolean hexists(final String key, final String field) {
		Jedis jedis = getJedis();

		Boolean reply = null;
		try {
			reply = jedis.hexists(key, field);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long hdel(final String key, final String... fields) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.hdel(key, fields);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long hlen(final String key) {
		Jedis jedis = getJedis();

		Long reply = null;
		try {
			reply = jedis.hlen(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Set<String> hkeys(final String key) {
		Jedis jedis = getJedis();

		Set<String> reply = null;
		try {
			reply = jedis.hkeys(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public List<String> hvals(final String key) {
		Jedis jedis = getJedis();

		List<String> reply = null;
		try {
			reply = jedis.hvals(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Map<String, String> hgetAll(final String key) {
		Jedis jedis = getJedis();

		Map<String, String> reply = null;
		try {
			reply = jedis.hgetAll(key);
			returnJedis(jedis);
		} catch (Exception e) {

			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public interface HScanner {
		void process(String field, String value);
	}

	public boolean hscan(String key, String pattern, HScanner scanner) {
		Jedis jedis = getJedis();
		try {
			String cursor = "0";
			ScanParams params = new ScanParams();
			params.match(pattern);
			do {
				ScanResult<Entry<String, String>> res = jedis.hscan(key,
						cursor, params);
				cursor = res.getStringCursor();
				for (Entry<String, String> entry : res.getResult()) {
					// 当需要失败时，抛出一个异常
					try {
						scanner.process(entry.getKey(), entry.getValue());
					} catch (Exception e) {
						e.printStackTrace();
						return false;
					}
				}
			} while (!"0".equals(cursor));
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}

		return true;
	}

	public Long zadd(String key, int score, String member) {
		Jedis jedis = getJedis();
		Long reply = null;
		try {
			reply = jedis.zadd(key, score, member);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Double zscore(String key, String member) {
		Jedis jedis = getJedis();
		Double reply = null;
		try {
			reply = jedis.zscore(key, member);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}

	public Long zrevrank(String key, String member) {
		Jedis jedis = getJedis();
		Long reply = null;
		try {
			reply = jedis.zrevrank(key, member);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}
	
	public Set<String> zrevrange(String key, int start, int end) {
		Jedis jedis = getJedis();
		Set<String> reply = null;
		try {
			reply = jedis.zrevrange(key, start, end);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}
	
	public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
		Jedis jedis = getJedis();
		Set<Tuple> reply = null;
		try {
			reply = jedis.zrevrangeWithScores(key, start, end);
			returnJedis(jedis);
		} catch (Exception e) {
			returnBrokenJedis(jedis);
		}
		return reply;
	}
	

}
